package cc.aiee.moneris.api;

import JavaAPI.Completion;
import JavaAPI.CvdInfo;
import JavaAPI.HttpsPostRequest;
import JavaAPI.PreAuth;
import JavaAPI.Purchase;
import JavaAPI.Receipt;
import JavaAPI.Refund;
import JavaAPI.Transaction;
import cc.aiee.core.bean.R;
import cc.aiee.core.kit.KitMoney;
import cc.aiee.moneris.config.MonerisPayConfigKit;
import cc.aiee.moneris.core.pojo.MonerisPayResponse;
import cc.aiee.moneris.core.tool.MonerisPayTool;
import cc.aiee.moneris.model.MonerisPayPreAuthModel;
import cc.aiee.moneris.model.MonerisPayPurchaseModel;
import cc.aiee.moneris.model.MonerisPayRefundModel;
import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MonerisPayApi {
	public Receipt send(Transaction transaction, boolean status_check) {
		HttpsPostRequest req = MonerisPayConfigKit.getDefault();
		req.setTransaction(transaction);
		req.setStatusCheck(status_check);
		req.send();
		Receipt receipt = req.getReceipt();
		return receipt;
	}

	public R<MonerisPayResponse> send2(Transaction transaction, boolean status_check) {
		Receipt receipt = send(transaction, false);

		MonerisPayResponse response = MonerisPayTool.convert(receipt);

		String responseCode = receipt.getResponseCode();

		return new R<>(responseCode, receipt.getMessage(), MonerisPayTool.isApproved(responseCode), response);
	}

	public R<MonerisPayResponse> purchase(MonerisPayPurchaseModel model) {

		if (log.isDebugEnabled()) {
			log.debug("MonerisPayPurchaseModel : \n{}", model.toJson(true, false));
		}
		Assert.notBlank(model.getOrderNo(), "order number is not blank,订单号不能为空");
		Purchase purchase = new Purchase();
		purchase.setOrderId(model.getOrderNo());

		Double amount = KitMoney.toYUAN(model.getAmount()).doubleValue();
		purchase.setAmount(amount.toString());

		purchase.setPan(model.getCardNo());

		// 过期日期,格式: YYMM
		String cardExpiryDate = model.getCardExpiryYear() + model.getCardExpiryMonth();

		purchase.setExpdate(cardExpiryDate);
		purchase.setCryptType(model.getCrypt());
		purchase.setDynamicDescriptor(model.getDescriptor());

		return send2(purchase, false);
	}

	public R<MonerisPayResponse> preAuth(MonerisPayPreAuthModel model) {

		// 过期日期,格式: YYMM
		String cardExpiryDate = model.getCardExpiryYear() + model.getCardExpiryMonth();
		Double amount = KitMoney.toYUAN(model.getAmount()).doubleValue();

		CvdInfo cvdCheck = new CvdInfo();
		/**
		 * CVD存在指示器：
		 * <li>0：故意绕过CVD值或商家未提供CVD值。</li>
		 * <li>1：存在CVD值。</li>
		 * <li>2：CVD值在卡上，但难以辨认。</li>
		 * <li>9：持卡人声明该卡没有CVD印记。</li>
		 */
		cvdCheck.setCvdIndicator("1");
		cvdCheck.setCvdValue(model.getCardCvd());

		PreAuth preauth = new PreAuth();
		preauth.setOrderId(model.getOrderNo());
		preauth.setAmount(String.valueOf(amount));
		preauth.setPan(model.getCardNo());
		preauth.setExpdate(cardExpiryDate);
//		preauth.setCryptType("7");
		preauth.setCryptType(model.getCrypt());
//		preauth.setAvsInfo(avsCheck);
		preauth.setCvdInfo(cvdCheck);
		return send2(preauth, false);
	}

	public R<MonerisPayResponse> preAuthCompletion(MonerisPayRefundModel model) {
		Double amount = KitMoney.toYUAN(model.getAmount()).doubleValue();
		
		Completion completion = new Completion();
		completion.setOrderId(model.getOrderNo());
		completion.setCompAmount(String.valueOf(amount));
		completion.setTxnNumber(model.getTxnNumber());
		completion.setCryptType(model.getCrypt());
//		completion.setCustId(cust_id);
		completion.setDynamicDescriptor(model.getDescriptor());
		return send2(completion,false);
	}
	public R<MonerisPayResponse> refund(MonerisPayRefundModel model) {
		Assert.notBlank(model.getOrderNo(), "order number is not blank,订单号不能为空");
		Refund refund = new Refund();

		Double amount = KitMoney.toYUAN(model.getAmount()).doubleValue();

		refund.setAmount(amount.toString());
		refund.setCryptType(model.getCrypt());
		refund.setDynamicDescriptor(model.getDescriptor());
		refund.setOrderId(model.getOrderNo());
		refund.setTxnNumber(model.getTxnNumber());

		return send2(refund, false);
	}

}
